﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TM.QWeibo.SDK.Objects;
using System.Collections.Generic;

namespace TM.QWeibo.Dal
{
    public class Message:Base
    {
        /// <summary>
        /// API
        /// </summary>
        readonly SDK.API.PrivateMessage _Api = new SDK.API.PrivateMessage(OAuthInstance);

        /// <summary>
        /// 实例
        /// </summary>
        public static Message Instance
        {
            get
            {
                return new Message();
            }
        }

        /// <summary>
        /// 收件箱
        /// </summary>
        /// <param name="pageflag">分页标识（0：第一页，1：向下翻页，2:向上翻页）</param>
        /// <param name="pagetime">本页起始时间（第一页：填0，向上翻页：填上一次请求返回的第一条记录时间，向下翻页：填上一次请求返回的最后一条记录时间）</param>
        /// <param name="callback"></param>
        public void Inbox(PageFlag pageflag, long pagetime, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            _Api.GetRecvList(pageflag, pagetime, Common.Params.PageListCount, (rs) => {
                var list = new List<SDK.Objects.Tweet>();
                if (rs != null && rs.Data != null && rs.Data.Info != null) {
                    list = rs.Data.Info;
                }
                callback(list);
            });
        }

        /// <summary>
        /// 指定用户发来的微博
        /// </summary>
        /// <param name="name">指定用户</param>
        /// <param name="pageflag">分页标识（0：第一页，1：向下翻页，2:向上翻页）</param>
        /// <param name="pagetime">本页起始时间（第一页：填0，向上翻页：填上一次请求返回的第一条记录时间，向下翻页：填上一次请求返回的最后一条记录时间）</param>
        /// <param name="callback"></param>
        public void Inbox(string name,PageFlag pageflag, long pagetime, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            this.Inbox(pageflag, pagetime, (rs) =>
            {
                rs = rs.Where(t => string.Compare(t.Name, name, StringComparison.CurrentCultureIgnoreCase) == 0).ToList();
                callback(rs);
            });
        }

        /// <summary>
        /// 发件箱
        /// </summary>
        /// <param name="pageflag">分页标识（0：第一页，1：向下翻页，2:向上翻页）</param>
        /// <param name="pagetime">本页起始时间（第一页：填0，向上翻页：填上一次请求返回的第一条记录时间，向下翻页：填上一次请求返回的最后一条记录时间）</param>
        /// <param name="reqnum">每次请求记录的条数（1-20条）</param>
        /// <param name="callback"></param>
        public void Sendbox(PageFlag pageflag, long pagetime, int reqnum, Action<ICollection<SDK.Objects.Tweet>> callback)
        {
            _Api.GetSendList(pageflag, pagetime, reqnum, (rs) =>
            {
                var list = new List<SDK.Objects.Tweet>();
                if (rs != null && rs.Data != null && rs.Data.Info != null)
                {
                    list = rs.Data.Info;
                }
                callback(list);
            });
        }

        /// <summary>
        /// 发表私信
        /// </summary>
        /// <param name="name">收信人</param>
        /// <param name="tweetContent">私信内容</param>
        /// <param name="callback">回调</param>
        public void Add(string name, SDK.Objects.TweetContent tweetContent, Action<bool> callback)
        {
            _Api.Add(name, tweetContent.Text, tweetContent.ClientIp, tweetContent.Jing, tweetContent.Wei, (rs) =>
            {
                callback(rs.Errcode == EErrcode.Success);
            });
        }
    }
}
